import { defineStore } from "pinia";
import { ref } from "vue";

export const useUserStore = defineStore(
  "user",
  () => {
    const userInfo = ref({});
    // 设置用户信息
    const setUserInfo = (val) => {
      userInfo.value = val;
    };
    // 获取用户信息
    const getUserInfo = () => {
      return userInfo.value;
    };
    // 清理用户信息，退出时使用
    const clearUserInfo = () => {
      userInfo.value = null;
    };
    return {
      userInfo,
      setUserInfo,
      getUserInfo,
      clearUserInfo,
    };
  },
  // 默认持续化保存
  {
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key);
        },
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
      },
    },
  }
);
